<template>
  <el-dialog width="1900px" v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form-item label="接口名称" prop="invokeId" style="background-color: #cbd4ab">
        <el-tag type="success">{{invokeRow.invokeName}}</el-tag>
      </el-form-item>
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <el-form-item>
        <el-button v-if="state.hasPermission('oatools:fields:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('oatools:fields:delete')" type="danger" @click="state.deleteHandle()">删除</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="addOrUpdateHandle()">查看json</el-button>
      </el-form-item>
    </el-form>
<!----------------------------------------------------自定义start------------------------------------------------------------->
      <el-table :data="fieldDataList" border style="width: 100%">
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
<!--        <el-table-column prop="id" label="ID" header-align="center" align="center"></el-table-column>-->
        <el-table-column prop="pid" label="父ID" header-align="center" align="center"></el-table-column>
<!--        <el-table-column prop="invokeId" label="接口ID" header-align="center" align="center"></el-table-column>-->
        <el-table-column prop="targetFields" label="目标-字段" header-align="center" align="center"></el-table-column>
        <el-table-column prop="targetType" label="目标-类型" header-align="center" align="center"></el-table-column>
        <el-table-column prop="targetDefault" label="目标-默认值" header-align="center" align="center"></el-table-column>
        <el-table-column prop="targetComment" label="目标-说明" header-align="center" align="center"></el-table-column>
        <el-table-column prop="transmitTable" label="传递-表" header-align="center" align="center"></el-table-column>
        <el-table-column prop="transmitTableName" label="传递-表名" header-align="center" align="center"></el-table-column>
        <el-table-column prop="transmitFields" label="传递-字段" header-align="center" align="center"></el-table-column>
        <el-table-column prop="transmitFieldsName" label="传递-字段名" header-align="center" align="center"></el-table-column>
        <el-table-column prop="transmitType" label="传递-类型" header-align="center" align="center"></el-table-column>
        <el-table-column prop="transmitComment" label="传递说明" header-align="center" align="center"></el-table-column>
        <el-table-column prop="replaceTypeGroovy" label="传递-Groovy" header-align="center" align="center"></el-table-column>
        <el-table-column prop="isDel" label="是否删除" header-align="center" align="center"></el-table-column>
<!--        <el-table-column prop="createDate" label="创建时间" header-align="center" align="center"></el-table-column>-->
<!--        <el-table-column prop="updateDate" label="修改时间" header-align="center" align="center"></el-table-column>-->
        <el-table-column label="操作" fixed="right" header-align="center" align="center" width="150">
          <template v-slot="scope">
            <el-button v-if="state.hasPermission('oatools:fields:update')" type="primary" link @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
            <el-button v-if="state.hasPermission('oatools:fields:delete')" type="primary" link @click="state.deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update ref="addOrUpdateRef" @refreshDataList="state.getDataList">确定</add-or-update>

      <!----------------------------------------------------自定义end------------------------------------------------------------->

  </el-dialog>
</template>

<script lang="ts" setup>
import {reactive, ref, toRefs} from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
import AddOrUpdate from "@/views/oatools/fields-add-or-update.vue";
import useView from "@/hooks/useView";

const view = reactive({
  deleteIsBatch: true,
  getDataListURL: "/oatools/fields/page",
  getDataListIsPage: true,
  exportURL: "/oatools/fields/export",
  deleteURL: "/oatools/fields"
});

const state = reactive({ ...useView(view), ...toRefs(view) });



const visible = ref(false);
const dataFormRef = ref();
const invokeList = ref<any[]>([]);

const invokeRow = ref<any[]>([]);
const fieldDataList = ref<any[]>([]);

const dataForm = reactive({
  id: '',  pid: '',  invokeId: '',  targetFields: '',  targetType: '',  targetDefault: '',  targetComment: '',  transmitTable: '',  transmitTableName: '',  transmitFields: '',  transmitFieldsName: '',  transmitType: '',  transmitComment: '',  replaceTypeGroovy: '',  isDel: '',  createDate: '',  updateDate: ''});

const rules = ref({
    //       pid: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
          invokeId: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
    //       targetFields: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       targetType: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       targetDefault: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       targetComment: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       transmitTable: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       transmitTableName: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       transmitFields: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       transmitFieldsName: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       transmitType: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       transmitComment: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       replaceTypeGroovy: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       isDel: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
      });

const init = (rowInvoke?: any) => {debugger
  invokeRow.value = rowInvoke;
  visible.value = true;
  dataForm.id = "";
  getInvokeIdList();

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (rowInvoke.id) {
    getInfo(rowInvoke.id);
    getFieldList(rowInvoke.id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/oatools/fields/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  })

};
// 获取信息
const getFieldList = (invokeId: number) => {
  const params = {
    invokeId: invokeId,
  };
  baseService.get("/oatools/fields/list", params).then((res) => {
    fieldDataList.value =res.data;
  });
}


// 获取系统信息
const getInvokeIdList = () => {
  baseService.get("/oatools/invoke/list").then((res) => {
    invokeList.value = res.data;
  });
};
const addOrUpdateRef = ref();
const addOrUpdateHandle = (id?: number) => {
  addOrUpdateRef.value.init(id);
};

defineExpose({
  init
});
</script>
